// https://autumnfish.cn/search

let player = new Vue({
    el: '#app',
    data: {
        searchMessage: '',
        songsMessage: [],
        musicIdList: [],
        mvIdList: [],
        musicListMessage: {},
        audioUrl: '',
        isAutoPlay: false,
        picUrl: '',
        isPaused: true,
        isPlayingMv: false,
        mvUrl: '',
        commentsContent: [],
        commentsPic: []
    },

    methods: {
        searchMusic() {
            let that = this;
            axios.get('https://autumnfish.cn/cloudsearch?keywords=' + that.searchMessage).then(
                function (response) {
                    // console.log(response);
                    that.songsMessage = response.data.result.songs;
                    that.musicIdList = that.songsMessage.map((item) => item.id);
                    that.mvIdIdList = that.songsMessage.map((item) => item.mvid);
                    that.musicListMessage.num = that.songsMessage.length;

                },
                function (err) {
                    console.log(err);
                }
            )
        },

        playMusic(index) {
            let that = this;
            this.musicListMessage.index = index;
            console.log(this.musicListMessage);
            // 获取歌曲url
            axios.get('https://autumnfish.cn/song/url?id=' + that.musicIdList[index]).then(
                function (response) {
                    //设置audio的url
                    that.audioUrl = response.data.data[0].url;
                    that.isAutoPlay = true;
                },
                function (err) {
                    console.log(err);
                }
            );


            //拿到封面
            axios.get('https://autumnfish.cn/song/detail?ids=' + that.musicIdList[index]).then(

                function (response) {
                    //设置封面，开始旋转
                    that.picUrl = response.data.songs[0].al.picUrl;
                    that.isPaused = false;
                },
                function (err) {
                    console.log(err);
                }
            );

            // 获取评论
            axios.get('https://autumnfish.cn/comment/music?id=' + that.musicIdList[index]).then(
                function (response) {
                    // console.log(response);
                    // 评论内容
                    that.commentsContent = response.data.comments.map((item) => item.content);

                    //评论头像
                    that.commentsPic = response.data.comments.map((item) => item.user.avatarUrl);

                },
                function (err) {
                    console.log(err);
                }
            )

        },
        setPause() {
            this.isPaused = true;
        },
        setPlay() {
            this.isPaused = false;
        },
        playMv(index) {
            console.log(1);
            let that = this;
            this.isPlayingMv = true;
            axios.post('https://autumnfish.cn/mv/url?id=' + that.mvIdList[index]).then(
                function (response) {
                    // console.log(response);
                    that.mvUrl = response.data.data.url;
                },
                function (err) {
                    console.log(err);

                }
            )
        },
        closeMv() {
            this.isPlayingMv = false;
            this.$refs.video.pause();
        },
        playNextMusic() {
            this.musicListMessage.index = (this.musicListMessage.index + 1) % this.musicListMessage.num;
            this.playMusic(this.musicListMessage.index);
        }


    }
})